<?php

namespace App\Module\Api\Controllers;

use App\Module\Api\Controllers\Request\CommonRequest;
use App\Module\Api\Controllers\Request\QueryRequest;
use App\Module\Api\Controllers\Request\TableRequest;
use App\Module\Api\Controllers\Request\TableWhereRequest;
use App\System\Base\Helpers\Traits\CommonController;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, CommonController;

    /**
     * api 总参数对像
     * @var CommonRequest
     */
    protected $apiJson;

    /**
     * api query 对像
     * @var
     */
    protected $query;

    /**
     * api table 对像
     * @var
     */
    protected $table;

    /** api where 对像
     * @var
     */
    protected $where;

    /**
     * 每页显示条数(设置了则以设置为准，不以移动端为准)
     * @var int
     */
    public $limit;

    public function __construct()
    {
        if (!$this->query) $this->query = new QueryRequest();
        if (!$this->table) $this->table = new TableRequest();
        if (!$this->where) $this->where = new TableWhereRequest();
        $this->setApiParameters();//接收API提交参数，并转化成对像
    }

    /**
     * 设置总参数
     * @return CommonRequest
     */
    public function setApiParameters()
    {
        $request = Request::all();
        $this->apiJson = new CommonRequest();
        $parameter = $this->apiJson->getObjectVars();
        foreach ($parameter as $k=>$v)
        {
            if($k == 'query')
            {
                if (isset($request['q'])) $this->setQuery($request['q']);
            }
            else
            {
                $this->apiJson->$k = isset($request[$v]) ? $request[$v] : '';
            }
        }
        return $this->apiJson;
    }

    /**
     * 设置query对像
     * @param $query
     */
    public function setQuery($query)
    {
        $parameter = $this->query->getObjectVars();
        if(!isset($query['ta'])) $this->setTable([]);//防止没有传table 对像，先设置默认值
        foreach ($parameter as $k=>$v)
        {
            if($k == 'table')
            {
                if(isset($query['ta'])) $this->setTable($query['ta']);
            }
            else
            {
                if(isset($query[$v]))
                {
                    $this->query->$k = $query[$v];
                }
                else
                {
                    $this->query->$k = isset($query[$k]) ? $query[$k] : '';
                }

            }
        }
        return $this->query;
    }

    /**
     * 设置table对像
     * @param $table
     */
    public function setTable($table)
    {
        $parameter = $this->table->getObjectVars();
        foreach ($parameter as $k=>$v)
        {
            $class_name = 'get'.ucfirst($k);
            if($k == 'where')
            {
                if(isset($table['w'])) $this->setTableWhere($table['w']);
            }
            else
            {
                $this->table->$k = isset($table[$v]) ? $this->$class_name($table[$v]) : $this->$class_name('');
            }
        }
        return $this->table;
    }

    /**
     * 设置where 对像
     * @param $where
     */
    public function setTableWhere($where)
    {
        $parameter = $this->where->getObjectVars();
        foreach ($parameter as $k=>$v)
        {
            if(isset($where[$v]) || isset($where[$k]))
            {
                $this->where->$k = isset($where[$v]) ? $where[$v] : $where[$k];
            }
            else
            {
                $this->where->$k = '';
            }

        }
        return $this->where;
    }

    /**
     *获取排序
     * @param $orderBy integer
     * @return string
     */
    private function getOrderBy($orderBy)
    {
        $orderBy = $orderBy ? $orderBy : '1';
        $order_by_array = $this->setOrderBy();
        return isset($order_by_array[$orderBy]) ? $order_by_array[$orderBy] : 'id';
    }

    /**
     * 设置默认排序字段，子类重写则应用子类的
     * @return array
     */
    public function setOrderBy()
    {
        return ['1'=>'id','2'=>'created_at'];
    }

    /**
     *获取排序
     * @param $orderType integer
     * @return string
     */
    private function getOrderType($orderType)
    {
        $orderType = $orderType ? $orderType : '1';
        $Order_type_array = $this->setOrderType();
        return $Order_type_array[$orderType];
    }

    /**
     * 设置排序,子类重写则应用子类的
     */
    public function setOrderType()
    {
        return [1=>'DESC',2=>'ASC'];
    }

    /**
     * 获取每页条目数
     * @param $limit int
     * @return int
     */
    private function getLimit($limit)
    {
        $limit = $limit ? $limit : '10';
        return $this->limit ? $this->limit : $limit;
    }

    /**
     *获取默认页码
     * @param $page int
     * @return int
     */
    private function getPage($page)
    {
        $page = $page ? $page : '1';
        return $page;
    }
}
